package com.atguigu.gulimall.product.service.impl;

import com.atguigu.gulimall.product.service.CategoryBrandRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.transaction.annotation.Transactional;


@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
    @Autowired
    private CategoryBrandRelationService relationService;



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<CategoryEntity> page = this.page(
                new Query<CategoryEntity>().getPage(params),
                new QueryWrapper<CategoryEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<CategoryEntity> listWithTree() {
        List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
        return categoryEntities.stream()
                .filter(e -> Objects.equals(0L, e.getParentCid()))
                .peek(e -> e.setChildCategoryList(getChildCategory(e, categoryEntities)))
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());

    }

    @Override
    public void removeMenuByIds(List<Long> idList) {
        baseMapper.deleteBatchIds(idList);
    }

    private List<CategoryEntity> getChildCategory(CategoryEntity root, List<CategoryEntity> all) {
        return all.stream().filter(e -> Objects.equals(e.getParentCid(), root.getCatId()))
                .peek(e -> e.setChildCategoryList(getChildCategory(e, all)))
                .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort())))
                .collect(Collectors.toList());
    }

    /**
     * 找到一个三级目录对应的完整路径 [2,25,225]  /电器/好电器/手机
     */
    @Override
    public Long[] findCatelogPath(Long catelogId) {
        //新建一个ArrayList集合 接受Long
        List<Long> paths = new ArrayList<>();
        //调用方法找到完整路径[225，22，2]
        List<Long> parentPath = findParentPath(catelogId, paths);
        //调用逆序方法获得正确顺序[2,25,225]
        Collections.reverse(parentPath);
        return parentPath.toArray(new Long[parentPath.size()]);
    }

    @Override
    @Transactional
    public void updateCascase(CategoryEntity category) {
        this.baseMapper.updateById(category);
        this.relationService.updateCategory(category.getCatId(), category.getName());
    }

    /**
     * 找到一个三级目录对应的完整路径 [2,25,225]  /电器/好电器/手机
     */
    private List<Long> findParentPath(Long catelogId, List<Long> paths) {
        //1、收集当前节点id
        paths.add(catelogId);
        //2 根据当前节点ID拿到其对应的实体类
        CategoryEntity byId = this.getById(catelogId);
        //3 当实体类父id不为0继续调用这个方法
        if (byId.getParentCid() != 0) {
            findParentPath(byId.getParentCid(), paths);
        }
        //4 跳出递归循环返回数据
        return paths;
    }
}